//引入自己封装后的request()
import { request } from '../../request/index.js'

Page({
    data: {
        swiperList: [],  //轮播图数组
        catesList: [],  //导航数组
        floorList: [],  //楼层数据
    },
    onLoad: function(options){
        this.getSwiperList()
        this.getCateList()
        this.getFloorList()
    },
    //发起请求，获取轮播图数据
    getSwiperList(){
        request({url: '/home/swiperdata'})
        .then(result => {
            
            let swiperList = result.data.message

            //将商品的跳转路径中的main改为index
            swiperList.forEach((v, i) => {
                //console.log(i.navigator_url);
                //console.log(i);
                swiperList[i].navigator_url = v.navigator_url.replace(/main/, "index")
            })
            //console.log(swiperList);

            this.setData({
                swiperList
            })
        })
    },
    //发起请求，获取分类导航数据
    getCateList(){
        request({url: '/home/catitems'})
        .then(result => {
            this.setData({
                catesList: result.data.message
            })
        })
    },
    //发起请求，获取楼层数据
    getFloorList(){
        request({url: '/home/floordata'})
        .then(result => {
            let floorList = result.data.message

            //在跳转url中 拼接上index
            floorList.forEach((v1, i1) => {
                //console.log(v1);
                v1.product_list.forEach((v2, i2) => {
                    //console.log(v2);
                    v1.product_list[i2].navigator_url = v2.navigator_url.replace(/\?/, "/index?")
                })
            })

            this.setData({
                floorList
            })
        })
    }
});

